package com.appheader.rongmaphone.utils;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

import com.appheader.rongmaphone.R;


/**
 * Created by lpc on 16/3/25.
 * 詳情頁底部，彈出是popwindow
 */
public  abstract class PopWindowUtil {
    private PopupWindow mPopupWindow;
    private Context mContext;
    private int popLayoutRes;

//    private boolean isShowDown;

    public void getPopupWindowInstance(int popLayout,Context context,int popWidth,int popHeight) {
        this.popLayoutRes = popLayout;
        mContext = context;
//        this.isShowDown = isDown;
        if (null != mPopupWindow) {
            mPopupWindow.dismiss();
            initPop(popLayoutRes,popWidth,popHeight);
        } else {
            initPop(popLayoutRes,popWidth,popHeight);
        }
    }
    private void initPop(int popLayoutRes,int popWidth,int popHeight){
        View popupWindow_view = LayoutInflater.from(mContext).inflate(popLayoutRes, null, false);

        mPopupWindow = new PopupWindow(popupWindow_view, popWidth,popHeight, true);

        // 点击其他地方消失
        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                    mPopupWindow = null;
                }
                return false;
            }
        });


        mPopupWindow.setOutsideTouchable(true);
//        if (isDown){
//            mPopupWindow.setAnimationStyle(R.style.DropdownMenuAnim);
//        }else {
//            mPopupWindow.setAnimationStyle(R.style.BottomMenuAnim);
//        }
        mPopupWindow.setAnimationStyle(R.style.BottomMenuAnim);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));
        initPopLayout(popLayoutRes,popupWindow_view);
    }
    public void showBottomPop(View  v){
        if (mPopupWindow != null){
            mPopupWindow.dismiss();
            mPopupWindow.showAtLocation(v, Gravity.BOTTOM,0,0);
        }

    }
    public void showBelow(View  v){
        mPopupWindow.setAnimationStyle(R.style.DropdownMenuAnim);
        if (mPopupWindow != null){
            mPopupWindow.dismiss();
            mPopupWindow.showAsDropDown(v);
        }

    }
    public void hidePop(){
        if (mPopupWindow != null){
            mPopupWindow.dismiss();
        }

    }

    public abstract void initPopLayout(int layout,View view);


}
